<?php

namespace app\admin\controller\wwh;

use app\common\controller\Backend;
use think\Db;

class Config extends Base
{
    // 表字段结构
    const DEFAULT_CONFIG = [
        'template' => 'default',
        'site_name' => '',
        'keywords' => '',
        'description' => '',
        'logo' => '',
        'logo1' => '',
        'footer_logo' => '',
        'tel' => '',
        'email' => '',
        'address' => '',
        'gongwang' => '',
        'link1' => '',
        'beian' => '',
        'link2' => '',
        'copyright' => '',
        'weibo' => '',
        'wechat' => '',
        'douyin' => '',
        'banner1' => '',
        'ban1_t1' => '',
        'banner2' => '',
        'ban2_t1' => '',
        'banner3' => '',
        'ban3_t1' => '',
        'banner4' => '',
        'ban4_t1' => '',
        'banner5' => '',
        'ban5_t1' => '',
        'banner6' => '',
        'ban6_t1' => '',
        'banner7' => '',
        'ban7_t1' => '',
        'content' => ''
    ];

    // 语言标识
    const LANG_CN = 1;
    const LANG_EN = 2;

    /**
     * 查看
     */
    public function index()
    {
        // 初始化中英文数据
        $dataCN = $this->initConfigData(self::LANG_CN);
        $dataEN = $this->initConfigData(self::LANG_EN);

        //获取模板名称
        $directory = ADDON_PATH . 'wwh' . DS . 'view' . DS;
        $files = scandir($directory);
        $folders = [];
        foreach ($files as $file) {
            if ($file !== '.' && $file !== '..' && is_dir($directory . '/' . $file)) {
                $folders[] = $file;
            }
        }

        $this->assign([
            'dataCN' => $dataCN,
            'dataEN' => $dataEN,
            'folders' => $folders
        ]);

        return $this->view->fetch();
    }

    /**
     * 初始化数据
     * @param int $lang 语言标识
     * @return array
     */
    protected function initConfigData($lang)
    {
        $id = $lang; // ID与语言标识相同
        $data = Db::name('wwh_config')->where('id', $id)->find();

        if (!$data) {
            $data = array_merge(
                ['id' => $id, 'lang' => $lang],
                self::DEFAULT_CONFIG
            );
            Db::name('wwh_config')->insert($data);
        }

        return $data;
    }

    /**
     * 保存数据
     * @param int $lang 语言标识
     */
    public function saveConfig($lang)
    {
        $id = $lang; // ID与语言标识相同
        $data = [
            'id' => $id,
            'template' => input('template'),
            'site_name' => input('site_name'),
            'keywords' => input('keywords'),
            'description' => input('description'),
            'logo' => input('logo'),
            'logo1' => input('logo1'),
            'footer_logo' => input('footer_logo'),
            'tel' => input('tel'),
            'email' => input('email'),
            'address' => input('address'),
            'gongwang' => input('gongwang'),
            'link1' => input('link1'),
            'beian' => input('beian'),
            'link2' => input('link2'),
            'copyright' => input('copyright'),
            'weibo' => input('weibo'),
            'wechat' => input('wechat'),
            'douyin' => input('douyin'),
            'banner1' => input('banner1'),
            'ban1_t1' => input('ban1_t1'),
            'banner2' => input('banner2'),
            'ban2_t1' => input('ban2_t1'),
            'banner3' => input('banner3'),
            'ban3_t1' => input('ban3_t1'),
            'banner4' => input('banner4'),
            'ban4_t1' => input('ban4_t1'),
            'banner5' => input('banner5'),
            'ban5_t1' => input('ban5_t1'),
            'banner6' => input('banner6'),
            'ban6_t1' => input('ban6_t1'),
            'banner7' => input('banner7'),
            'ban7_t1' => input('ban7_t1'),
            'content' => input('content'),
            'lang' => $lang
        ];

        $exists = Db::name('wwh_config')->where('id', $id)->find();

        if ($exists) {
            // 检查数据变动
            foreach (array_keys(self::DEFAULT_CONFIG) as $field) {
                if ($exists[$field] != $data[$field]) {
                    $result = Db::name('wwh_config')->update($data);
                    return $this->returnResult($result);
                }
            }
            return $this->error('未检测到数据变动', null, null, false);
        }

        $result = Db::name('wwh_config')->insert($data);
        return $this->returnResult($result);
    }

    /**
     * 返回操作结果
     * @param bool $result
     */
    protected function returnResult($result)
    {
        return $result ? $this->success('保存成功') : $this->error('保存失败');
    }

    /**
     * 中文站点配置修改
     */
    public function ConfigCN()
    {
        return $this->saveConfig(self::LANG_CN);
    }

    /**
     * 英文站点配置修改
     */
    public function ConfigEN()
    {
        return $this->saveConfig(self::LANG_EN);
    }
}
